package delta;

import java.util.ArrayList;
import java.util.Random;

public class Fog implements IFieldable {
	
	
	private Integer fogSteps;
	private Field currentField;
	private Boolean testModeOn;
	public Boolean isDisappearAutomatically;
	

	private Random rand = new Random();
	private int move;

	public Fog(Field field) {
	
		
		testModeOn = false;
		currentField = field;
		currentField.addObject(this);
		fogSteps = 100;
		isDisappearAutomatically=true;

	}

	public Fog(Field field, Boolean testMode, Boolean isDisappearAutomatically) {

		this.isDisappearAutomatically = isDisappearAutomatically;
		testModeOn = testMode;
		currentField = field;
		currentField.addObject(this);
		fogSteps = 100;

	}

//	@Override
	public void update() {
		//a tornyok kodositese
		ArrayList<IFieldable> list1 = currentField.getObjects();
		// ehhez uj parameter
		ActionParameters ap=new ActionParameters();
		ap.setFoggy(true);
		// a parameter ervenyesitese az elemeken
		for (IFieldable o : list1) {
					o.action(ap);
		}
		
		//Ha lejar az ido, a kod tovabbmegy
		if (fogSteps <= 0) {
			fogSteps = 100;

			// A kod egy bizonyos valoszinuseggel eltunik mozgaskor
			if (isDisappearAutomatically) {
				int rnd = rand.nextInt(100);
				if (rnd >= 89){
					//eltavolitas elott is vissza kell allitani a tornyokat
					//kodmentes allapotba
					ap.setFoggy(false);
					for (IFieldable o : list1) {
						o.action(ap);
					}
					currentField.removeObject(this);
					currentField=null;
					return;
				}
							
			}

			ArrayList<Field> flist = currentField.getSurroundingFields();

			// Ha nem teszt modba van veletlenszeruen mozog
			if (testModeOn == false) {
				move = rand.nextInt(flist.size());
			}

			// A tornyok megszabaditasa a kodtol
			ArrayList<IFieldable> list = currentField.getObjects();

			
			ap.setFoggy(false);

			// Csak a tornyok erzekelik
			for (IFieldable o : list) {

				o.action(ap);
			}

			// A kod uj helyre mozgatasa
			currentField.removeObject(this);
			currentField = flist.get(move);
			currentField.addObject(this);

			
		} else {
			fogSteps--;
		}

	}

	public void setMove(int move) {
		this.move = move;
	}

	public void remove() {
		currentField.removeObject(this);
		currentField = null;
		
	}

	@Override
	public boolean action(ActionParameters params) {
		if(params==null)return true;
		return false;

	}

	public String toString() { // Ha kell egeszitse ki valaki

		return "";
	}

	@Override
	public String getMapchar() {
		// TODO Auto-generated method stub
		return new String("F");

	}

	@Override
	public Field getField() {
		// TODO Auto-generated method stub
		return currentField;
	}

}
